package com.teatrace.commons.util;

import java.io.File;
import java.lang.reflect.Proxy;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.AnnotationConfiguration;
import org.hibernate.cfg.Configuration;

import com.teatrace.commons.hibernate3.support.SaveAndRefreshObjectSessionProxy;

public class HibernateUtils {

	private static final String CONFIG_FILE = "hibernate.cfg.xml";

	private static SessionFactory sessionFactory;

	private static Configuration configuration;

	static {
		String filePath = HibernateUtils.class.getResource("/") + CONFIG_FILE;
		File file = new File(filePath.replace("file:/", ""));
		if (file.exists()) {
			sessionFactory = new AnnotationConfiguration().configure(file)
					.buildSessionFactory();
		} else {
			sessionFactory = new AnnotationConfiguration().configure()
					.buildSessionFactory();
		}

		configuration = new AnnotationConfiguration().configure(CONFIG_FILE);
		sessionFactory = configuration.buildSessionFactory();
	}

	public static SessionFactory getSessionFactory() {
		return sessionFactory;
	}

	public static Configuration getConfiguration() {
		return configuration;
	}

	/**
	 * 此方法在这里禁用
	 */
	@Deprecated
	public static Session openSession() {
		return sessionFactory.openSession();
	}

	public static Session getCurrentSession() {
		Session session = sessionFactory.getCurrentSession();
		return session;
	}

}
